package com.hnluchuan.bmjlearning.dao;

import com.hnluchuan.bmjlearning.common.DeleteStatus;
import com.hnluchuan.bmjlearning.dto.AgentServicePackageDTO;
import com.hnluchuan.bmjlearning.model.AgentServicePackage;
import com.hnluchuan.core.dao.BaseDAO;
import com.hnluchuan.core.support.Page;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;

@Repository
public class AgentServicePackageDAO extends BaseDAO<AgentServicePackage> {

    public List<AgentServicePackage> find(AgentServicePackageDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From AgentServicePackage n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getUser() != null) {
        	if (dto.getUser().getId() != null) {
	            hql.append(" and n.user.id = ? ");
	            paras.add(dto.getUser().getId());
        	}
        }			
        if (dto.getAgent() != null) {
        	if (dto.getAgent().getId() != null) {
	            hql.append(" and n.agent.id = ? ");
	            paras.add(dto.getAgent().getId());
        	}
        }			
        if (dto.getServicePackage() != null) {
        	if (dto.getServicePackage().getId() != null) {
	            hql.append(" and n.servicePackage.id = ? ");
	            paras.add(dto.getServicePackage().getId());
        	}
        }			
        hql.append(" order by n.id desc ");
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public AgentServicePackage findOne(AgentServicePackageDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<AgentServicePackage> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

	public AgentServicePackage findByServicePackageIdAndAgentUserId(Long servicePackageId, Long agentUserId){
		String hql = "From AgentServicePackage a where a.servicePackage.id = ? and a.user.id = ? and a.deleted = ?";
		List<Object> paras = new ArrayList<>();
		paras.add(servicePackageId);
		paras.add(agentUserId);
		paras.add(DeleteStatus.NotDeleted.getValue());

		return super.findOne(hql, paras.toArray());
	}

	public Double getValidServicePackageCount(Long agentUserId, Long servicePackageId){
		StringBuilder hql = new StringBuilder();
		hql.append("Select sum(a.count) From AgentServicePackage a where a.deleted = ?");
		List<Object> paras = new ArrayList<>();
		paras.add(DeleteStatus.NotDeleted.getValue());
		if(agentUserId != null){
			hql.append(" and a.user.id = ? ");
			paras.add(agentUserId);
		}
		if(servicePackageId != null){
			hql.append(" and a.servicePackage.id = ? ");
			paras.add(servicePackageId);
		}

		return super.sumByHql(hql.toString(), paras.toArray());
	}

    public List<AgentServicePackage> findByUserId(Long userId){
    	String hql = "From AgentServicePackage a where a.user.id = ? and a.deleted = ?";
    	List<Object> paras = new ArrayList<>();
    	paras.add(userId);
    	paras.add(DeleteStatus.NotDeleted.getValue());

    	return super.find(hql, paras.toArray(), null);
	}

	/**
	 * 删除的也一样查出
	 * */
	public List<AgentServicePackage> findAllIncludeHistoryByUserId(Long userId){
		String hql = "From AgentServicePackage a where a.user.id = ? group by a.servicePackage.id";
		List<Object> paras = new ArrayList<>();
		paras.add(userId);
		return super.find(hql, paras.toArray(), null);
	}

	public int deleteAllAgentServicePackageByUserId(Long userId){
		String hql = "Update AgentServicePackage a set a.deleted = ? where a.user.id = ?";
		List<Object> paras = new ArrayList<>();
		paras.add(DeleteStatus.Deleted.getValue());
		paras.add(userId);

		return super.executeUpdate(hql, paras.toArray());
	}
    
    @Override
	public Long create(AgentServicePackage agentServicePackage) {
	    if (agentServicePackage.getCreated() == null) {    
	        agentServicePackage.setCreated(new Date());
	    }    
		return super.create(agentServicePackage);
	}

	@Override
	public void update(AgentServicePackage agentServicePackage) {
		super.update(agentServicePackage);
	}
    
}
